package cn.kgc.xinwen.controller;

import cn.kgc.xinwen.pojo.Detail;
import cn.kgc.xinwen.pojo.ResultEntity;
import cn.kgc.xinwen.service.DetailService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-05-29 8:59
 */
@RestController
public class DetailController {

    @Resource
    DetailService detailService;

    @RequestMapping("/pageInfo")
    public ResultEntity<PageInfo<Detail>> pageInfo(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "3") Integer pageSize,
            @RequestParam(value = "categoryId",required = false,defaultValue = "0") Integer categoryId,
            @RequestParam(value = "title",required = false,defaultValue = "") String title
    ){
        PageInfo<Detail> pageInfo = detailService.pageInfo(pageNum, pageSize, categoryId, title);
        return  ResultEntity.successWithoutData(pageInfo);
    }

    //根据id查询
    @RequestMapping("/selectById")
    public ResultEntity<Detail> selectById(Integer id){
        Detail detail = detailService.selectById(id);
        return ResultEntity.successWithoutData(detail);
    }

    //修改
    @RequestMapping("/update")
    public ResultEntity<String> update(@RequestBody Detail detail){
        try {
            if(detail.getId()!=null){
                Integer update = detailService.update(detail);
                return ResultEntity.successWithoutData(update+"");
            }else{
               return ResultEntity.successWithoutData();
            }
        } catch (Exception e) {
            return ResultEntity.failed(e.getMessage());
        }
    }

}
